package com.example.springbootdemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootdemo.entity.User;
import com.example.springbootdemo.mybatisplus.IUserService;
import com.example.springbootdemo.sevice.UserService;
import com.example.springbootdemo.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  用户控制器
 * </p>
 *
 * @author yurenwei
 * @since 2023/9/7
 */

@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserService userService;

    @ApiOperation("新增")
    @PostMapping("/add")
    public Result add(@RequestBody User params){
        userService.add(params);
        return Result.ok();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public Result update(@RequestBody User params){
        userService.update(params);
        return Result.ok();
    }

    @ApiOperation("详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long")
    })
    @GetMapping("/queryDetail/{id}")
    public Result<User> update(@PathVariable Long id){
        return Result.ok(userService.queryDetails(id));
    }

    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long")
    })
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        userService.delete(id);
        return Result.ok();
    }

    @ApiOperation("查询所有")
    @GetMapping("/getAll")
    public Result<List<User>> getAll(){
        return Result.ok(userService.getAll());
    }

    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "size", value = "每页记录数", required = true, dataType = "Long")
    })
    @GetMapping("/page")
    public Result<Page<User>> page(@RequestParam Long current,@RequestParam Long size){
        Page<User> pageParams = new Page<>(current, size);
        return Result.ok(iUserService.page(pageParams));
    }

    @ApiOperation("导入")
    @PostMapping("/importUser")
    public Result importUser(@RequestParam(value = "file") MultipartFile file){
        userService.importUser(file);
        return Result.ok();
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response){
        userService.export(response);
    }

}
